<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px">
    <el-form-item label="新密码" prop="newPassword">
      <el-input v-model="user.newPassword" placeholder="请输入新密码" type="password"/>
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input v-model="user.confirmPassword" placeholder="请确认密码" type="password"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">修改密码</el-button>
    </el-form-item>
  </el-form>
</template>


<script>
  import crypto from 'crypto'  //引入依赖
  export default {
    data() {
      const equalToPassword = (rule, value, callback) => {
        if (this.user.newPassword !== value) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      };
      return {
        test: "1test",
        user: {
          newPassword: undefined,
          confirmPassword: undefined
        },
        // 表单校验
        rules: {
          newPassword: [
            {required: true, message: "新密码不能为空", trigger: "blur"},
            {min: 6, max: 10, message: "长度在 6 到 10 个字符", trigger: "blur"}
          ],
          confirmPassword: [
            {required: true, message: "确认密码不能为空", trigger: "blur"},
            {required: true, validator: equalToPassword, trigger: "blur"}
          ]
        }
      };
    },
    methods: {
      submit() {
        // console.log(this.$route.query.id)
        // console.log(this.$route.query.pwd)
        // console.log(this.$route.query.role)
        const md5 = crypto.createHash('md5')
        md5.update(this.user.confirmPassword,)
        let md5password = md5.digest('hex')  //md5password为加密后的内容，可直接传递给后端
        this.$http.post('update_user', null, {
          params: {
            id: this.$route.query.id,
            pwd: md5password,
            role: this.$route.query.role,
          }
        }).then(resp => {
          // console.log(resp.data)
          if (resp.data) {
            this.$alert('修改成功！', '修改成功！', {
              confirmButtonText: '确定',
            }).then(resp1 => this.$router.go(-1))
          }
        })
        ;
      },
    }
  };
</script>
<style scoped>

</style>
